<?php
namespace app\admin\controller;


use app\model\ArticleModel;
use app\model\CommentModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class comment{
    public function comment_list(){
        $rubbush = Request::param('rubbush') ?? '1';

        $commentList = CommentModel::where('rubbush','=',$rubbush)->select();
        View::assign('rubbush' , $rubbush);
        return View::fetch('',['commentList'=>$commentList]);

    }
    //移入回收站
    public function rubbush(){
        $commentid = Request::param('comment_id');
        $validate = Validate::rule([
            'comment_id|评论编号' => 'require|between:0 ,' .PHP_INT_MAX
        ]);
        $check = $validate->check(['comment_id'=>$commentid]);
        if(!$check){
            $date = [
                'status' => 10001,
                'message' => $validate->getError(),
                'date' => []
            ];
            return json($date);
        }
        $comment = CommentModel::find($commentid);

        if(empty($comment)){
            $date = [
                'status' => 10002,
                'message' => '该评论不存在',
                'date' => []
            ];
            return json($date);

        }
        $comment['rubbush'] = 2;
        $resule = $comment->save();
        if($resule){
            $date = [
                'status' => 0,
                'message' => '操作成功',
                'date' => []
            ];
            return json($date);
        }else{
            $date = [
                'status' => 10003,
                'message' => '操作失败',
                'date' => []
            ];
            return json($date);

        }
    }

    //恢复
    public function recovery(){
        $commentid = Request::param('comment_id');
        $validate = Validate::rule([
            'comment_id|评论编号' => 'require|between:0 ,' .PHP_INT_MAX
        ]);
        $check = $validate->check(['comment_id'=>$commentid]);
        if(!$check){
            $date = [
                'status' => 10001,
                'message' => $validate->getError(),
                'date' => []
            ];
            return json($date);
        }
        $comment = CommentModel::find($commentid);

        if(empty($comment)){

            $date = [
                'status' => 10002,
                'message' => '该数据不存在',
                'date' => []
            ];
            return json($date);
        }
        $comment['rubbush'] = 1;
        $resule = $comment->save();
        if($resule){
            $date = [
                'status' => 0,
                'message' => '操作成功',
                'date' => []
            ];
            return json($date);
        }else{
            $date = [
                'status' => 10003,
                'message' => '操作失败',
                'date'=> []
            ];
            return json($date);
        }
    }

    //彻底删除
    public function delete(){
        $comment = Request::param('comment_id');
        $validate = Validate::rule([
            'comment_id|评论编号' => 'require|between:0,' .PHP_INT_MAX
        ]);
        $check = $validate->check(['comment_id' => $comment]);
//        var_dump($check);
        if(!$check){
            $date = [
                'status' => 10001,
                'message' => $validate->getError(),
                'date' => []
            ];
            return json($date);
        }

        $require = CommentModel::destroy($comment);
        if($require){
            $date = [
                'status' => 0,
                'message' => '删除成功',
                'date' => ''
            ];
            return json($date);
        }else{

            $date = [
                'status' => 10002,
                'message' => '删除失败',
                'date' => ''
            ];
            return json($date);
        }

    }
}